package com.future.hedongjun.home.fragment;


import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.future.baselib.entity.BaseResponse;
import com.future.baselib.http.HttpRequest;
import com.future.baselib.utils.GlideUtils;
import com.future.hedongjun.R;
import com.future.hedongjun.common.MyApp;
import com.future.hedongjun.home.bean.response.ClassIntroduceResponse;
import com.future.hedongjun.mine.ui.LoginActivity;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * 创建日期：2018/11/19 17:56
 * author liu.p.f
 * 类说明： 课程介绍
 */
public class ClassIntroduceFragment extends Fragment {
    Unbinder unbinder;
    @BindView(R.id.tv_classIntroduceDetail)
    TextView tvClassIntroduceDetail;
    @BindView(R.id.iv_classIntroduceIcon)
    ImageView ivClassIntroduceIcon;
    private String classId;

    public ClassIntroduceFragment() {
        // Required empty public constructor
    }


    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_class_introduce, container, false);
        unbinder = ButterKnife.bind(this, view);
        initView();
        return view;
    }

    private void initView() {
        if (TextUtils.isEmpty(MyApp.getInstance().getUserToken())) {
            startActivity(new Intent(getContext(), LoginActivity.class));
        } else {
            new HttpRequest<ClassIntroduceResponse>()
                    .with(getContext())
                    .setApiCode("_classdetail_001")
                    .addParam("id", classId)
                    .addParam("userToken", MyApp.getInstance().getUserToken())
                    .addSuccessListener(new HttpRequest.OnNetworkSuccess() {
                        @Override
                        public void onSuccess(BaseResponse response) {
                            tvClassIntroduceDetail.setText(((ClassIntroduceResponse) response).data.get(0).introduction);
                            GlideUtils.loadImage(getContext(), ((ClassIntroduceResponse) response).data.get(0).imgUrl, ivClassIntroduceIcon);
                        }
                    }).start(new ClassIntroduceResponse());
        }
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }
    public void setClassId(String classId) {
        this.classId = classId;
    }
}
